package cc.lucien.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBHelper {
    //定义成员变量
    private static Connection connection = null;
    private static PreparedStatement preparedStatement = null;
    private static ResultSet resultSet = null;
    private static String DRIVER;
    private static String URL;
    private static String USER;
    private static String PASSWORD;

    /**
     * 加载配置文件
     * 加载驱动类
     */
    static {
        try {
            //加载配置文件
            Properties properties = new Properties();
            InputStream inputStream = DBHelper.class.getResourceAsStream("db.properties");
            properties.load(inputStream);
            //赋值
            DRIVER = properties.getProperty("driver");
            URL = properties.getProperty("url");
            USER = properties.getProperty("username");
            PASSWORD = properties.getProperty("password");
            //加载驱动类
            Class.forName(DRIVER);
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("加载配置文件失败");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.err.println("加载驱动类失败");
        }
    }

    /**
     * 获取数据库连接
     * @return
     */
    public static Connection getConnection(){
        try {
            connection = DriverManager.getConnection(URL,USER,PASSWORD);
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("获取数据库连接失败");
        }
        return null;
    }

    /**
     * 查询方法
     * @param sql
     * @param params
     * @return
     */
    public static ResultSet executeQuery(String sql, Object... params) {
        try {
            //1.获取连接
            getConnection();
            //2.预处理 sql
            preparedStatement = connection.prepareStatement(sql);
            //3.设置值
            setParams(params);
            //4.执行并返回结果
            return preparedStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 增、删、改方法
     * @param sql
     * @param params
     * @return
     */
    public static int executeUpdate(String sql, Object... params) {
        try {
            //1.获取连接
            getConnection();
            //2.预处理 sql
            preparedStatement = connection.prepareStatement(sql);
            //3.赋值
            setParams(params);
            //4.执行并返回结果
            return preparedStatement.executeUpdate();
        } catch (SQLException e){
            e.printStackTrace();
        } finally {
            close();
        }
        return -1;
    }

    /**
     * 赋值
     * @param params
     */
    private static void setParams(Object[] params) {
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                try {
                    preparedStatement.setObject(i+1, params[i]);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 释放资源
     */
    public static void close(){
        try {
            if (resultSet != null && !resultSet.isClosed()){
                resultSet.close();
                resultSet = null;
            }
            if (preparedStatement != null && !preparedStatement.isClosed()) {
                preparedStatement.close();
                preparedStatement = null;
            }
            if (connection != null && !connection.isClosed()) {
                connection.close();
                connection = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
